#!/usr/local/bin/python

import os
import re
import codecs
import collections

from readerconf import *;
import cburglish

syllableDist = collections.defaultdict(lambda : 0)


def analyze_extract(extract):
	global syllable

	ef = codecs.open(extract, encoding='utf-8')
	title = ef.readline()	# Title
	author = ef.readline()	# Author
	tags = ef.readline()	# Tags
	link = ef.readline()	# Link
	summary = ef.readline()	# Summary
	ef.close()

	if len(summary) > 0:
		try:
			summary = re.sub(summary, '<[a-z]+?>', '', re.U)
			summary = re.sub(summary, '<[a-z]+?/>', '', re.U)
		except StandardError:
			print summary
			raw_input("NEXT");
		syllables = cburglish.syllable(unicode(summary))
		for syllable in syllables.split('|'):
			syllable = syllable.strip()	
			if len(syllable) > 0:
				syllableDist[syllable] += 1

	
def report_analysis():
	mf = codecs.open(MODEL_FILE, 'w', encoding='utf-8')
	mf.write('<html><body><font face="Myanmar3">')
	for syllable in syllableDist.keys():
		mf.write('%s : %d<br>' % (syllable, syllableDist[syllable]))
	mf.write('</font></body></html>')
	mf.close()


if __name__ == '__main__':

	for extract in os.listdir(EXTRACTS_PATH):
		if extract.endswith('.txt'):
			print 'Analyzing %s ...' % extract
			analyze_extract(os.path.join(EXTRACTS_PATH, extract))
			print 'Done'
	report_analysis()



